package leetcode._203;

class Solution {
	public ListNode removeElements(ListNode head, int val) {
		// 3->null val=3
		// 3->3->null val=3
		while (head != null && head.val == val) {
			head = head.next;
		}

		if (head == null) {
			return head;
		}

		ListNode prev = head;
		while (prev.next != null) {
			if (prev.next.val == val) {
				prev.next = prev.next.next;
			} else {
				prev = prev.next;
			}
		}

		return head;
	}

}
